package com.elder.business.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyApartmentFeesDTO;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.OrderInfo;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.netty.util.concurrent.CompleteFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderlyApartmentFeesMapper;
import com.elder.business.domain.ElderlyApartmentFees;
import com.elder.business.service.IElderlyApartmentFeesService;

/**
 * 收费信息Service业务层处理
 * 
 * @author elder
 * @date 2023-11-26
 */
@Service
public class ElderlyApartmentFeesServiceImpl implements IElderlyApartmentFeesService 
{
    @Autowired
    private ElderlyApartmentFeesMapper elderlyApartmentFeesMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    @Autowired
    private OrderStatisticsServiceImpl orderStatisticsService;

    /**
     * 查询收费信息
     * 
     * @param id 收费信息主键
     * @return 收费信息
     */
    @Override
    public JSONObject selectElderlyApartmentFeesById(Long id)
    {
        ElderlyApartmentFees v = elderlyApartmentFeesMapper.selectElderlyApartmentFeesById(id);
        JSONObject obj = new JSONObject();
        obj.put("id", StringNewUtils.long2Str(v.getId()));
        obj.put("memberName", StringNewUtils.formatStr(v.getMemberName()));
        obj.put("feeType", v.getFeeType());
        obj.put("amount", v.getAmount());
        obj.put("paymentDate", StringNewUtils.formatStr(DateUtils.dateStrMIAO(v.getPaymentDate())));
        obj.put("paymentMethod", v.getPaymentMethod());
        obj.put("notes", StringNewUtils.formatStr(v.getNotes()));
//        obj.put("createdTime", DateUtils.dateStrMIAO(v.getCreatedTime()));
        return obj;
    }

    /**
     * 查询收费信息列表
     * 
     * @param elderlyApartmentFees 收费信息
     * @return 收费信息
     */
    @Override
    public List<ElderlyApartmentFees> selectElderlyApartmentFeesList(ElderlyApartmentFees elderlyApartmentFees)
    {
        return elderlyApartmentFeesMapper.selectElderlyApartmentFeesList(elderlyApartmentFees);
    }

    /**
     * 新增收费信息
     * 
     * @param feeDTO 收费信息
     * @return 结果
     */
    @Override
    public int insertElderlyApartmentFees(ElderlyApartmentFeesDTO feeDTO){
        ElderlyApartmentFees fees = new ElderlyApartmentFees();
        BeanUtils.copyProperties(feeDTO, fees);
        fees.setId(SnowGenerator.getId());
        fees.setPaymentDate(DateUtils.getDateSeconds(feeDTO.getPaymentDate()));
        fees.setCreatedTime(DateUtils.getLongTime());
        fees.setUpdatedTime(DateUtils.getLongTime());

        CompletableFuture.runAsync(()->{
            //全量统计金额
            Long monthStartTime = DateUtils.getMonthStartTime();
            Long monthEndTime = DateUtils.getMonthEndTime();
            //当月订单数量
            ElderlyApartmentFees orders = elderlyApartmentFeesMapper.getCurrentMonthFees(monthStartTime, monthEndTime);
            orderStatisticsService.updateOrderInfo(monthStartTime,fees.getAmount());
        });


        return elderlyApartmentFeesMapper.insertElderlyApartmentFees(fees);
    }

    /**
     * 修改收费信息
     * 
     * @param feeDTO 收费信息
     * @return 结果
     */
    @Override
    public int updateElderlyApartmentFees(ElderlyApartmentFeesDTO feeDTO)
    {
        ElderlyApartmentFees fees = new ElderlyApartmentFees();
        BeanUtils.copyProperties(feeDTO, fees);
        fees.setPaymentDate(DateUtils.getDateSeconds(feeDTO.getPaymentDate()));
        fees.setUpdatedTime(DateUtils.getLongTime());
        return elderlyApartmentFeesMapper.updateElderlyApartmentFees(fees);
    }

    /**
     * 批量删除收费信息
     * 
     * @param ids 需要删除的收费信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentFeesByIds(Long[] ids)
    {
        return elderlyApartmentFeesMapper.deleteElderlyApartmentFeesByIds(ids);
    }

    /**
     * 删除收费信息信息
     * 
     * @param id 收费信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentFeesById(Long id)
    {
        return elderlyApartmentFeesMapper.deleteElderlyApartmentFeesById(id);
    }

    @Override
    public TableDataInfo getElderFeeInfoList(String memberName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ElderlyApartmentFees> list = elderlyApartmentFeesMapper.getElderFeeInfoList(memberName);
        PageInfo<ElderlyApartmentFees> page = new PageInfo<>(list);
        List<ElderlyApartmentFees> feesList = page.getList();
        List<JSONObject> feesInfoList = feesList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("id", StringNewUtils.long2Str(v.getId()));
            obj.put("memberName", StringNewUtils.formatStr(v.getMemberName()));
            obj.put("feeType", v.getFeeType());
            obj.put("amount", v.getAmount());
            obj.put("paymentDate", StringNewUtils.formatStr(DateUtils.dateStrMIAO(v.getPaymentDate())));
            obj.put("paymentMethod", v.getPaymentMethod());
            obj.put("notes", StringNewUtils.formatStr(v.getNotes()));
            obj.put("createdTime", DateUtils.dateStrMIAO(v.getCreatedTime()));
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo result = new TableDataInfo();
        result.setTotal(page.getTotal());
        result.setCode(200);
        result.setMsg("success");
        result.setRows(feesInfoList);
        return result;
    }
}
